/**
 * this should come from a config file
 * Ideally we could have an object with all needed variables of an institution parameterized;
 * wms server, tilecaching server, download server, source graphic...what else? resolutions array for tile caching?
 */

if (typeof org === 'undefined'){ 
	org = {};
} else if (typeof org !== "object"){
	throw new Error("org already exists and is not an object");
}

// Repeat the creation and type-checking code for the next level
if (typeof org.OpenGeoPortal === 'undefined'){
	org.OpenGeoPortal = {};
} else if (typeof org.OpenGeoPortal !== "object"){
    throw new Error("org.OpenGeoPortal already exists and is not an object");
}

if (typeof org.OpenGeoPortal.InstitutionInfo === 'undefined'){
	org.OpenGeoPortal.InstitutionInfo = {};
} else if (typeof org.OpenGeoPortal.InstitutionInfo !== "object"){
    throw new Error("org.OpenGeoPortal.InstitutionInfo already exists and is not an object");
}

org.OpenGeoPortal.InstitutionInfo.Config = {};
org.OpenGeoPortal.InstitutionInfo.Search = {};
org.OpenGeoPortal.InstitutionInfo.homeInstitution = "";
org.OpenGeoPortal.InstitutionInfo.institutionSpecificCss = "";


org.OpenGeoPortal.InstitutionInfo.getHomeInstitution = function(){	
	var institution = org.OpenGeoPortal.InstitutionInfo.homeInstitution;
	//return homeInstitution if it is not empty
	if (institution.length > 0){
		return institution;
	} else {
		org.OpenGeoPortal.InstitutionInfo.requestInfo();
		return org.OpenGeoPortal.InstitutionInfo.homeInstitution;
	}
};

org.OpenGeoPortal.InstitutionInfo.getCustomCss = function(){	
	var institution = org.OpenGeoPortal.InstitutionInfo.institutionSpecificCss;
	//return institutionSpecificCss if it is not empty
	if (institution.length > 0){
		return institution;
	} else {
		org.OpenGeoPortal.InstitutionInfo.requestInfo();
		return org.OpenGeoPortal.InstitutionInfo.institutionSpecificCss;
	}
};

org.OpenGeoPortal.InstitutionInfo.getSearch = function(){	
	var search = org.OpenGeoPortal.InstitutionInfo.Search;
	//return Search if it is not empty
	for (var i in search){
		return search;
	}

	org.OpenGeoPortal.InstitutionInfo.requestInfo();
	return org.OpenGeoPortal.InstitutionInfo.Search;
	
};

org.OpenGeoPortal.InstitutionInfo.getInstitutionInfo = function(){
	var configObj = org.OpenGeoPortal.InstitutionInfo.Config;
	//return the configObj if it is not empty
	for (var i in configObj){
		return configObj;
	}
	//otherwise, get set the configObj from the xml config file
	org.OpenGeoPortal.InstitutionInfo.requestInfo();
	return org.OpenGeoPortal.InstitutionInfo.Config;
};

org.OpenGeoPortal.InstitutionInfo.requestInfo = function(){
	var params = {
		url: "ogpConfig.js",
		async: false,
		dataType: 'json',
		success: function(data){
			var institutions = data["config"]["institutions"];
			org.OpenGeoPortal.InstitutionInfo.Search = data["config"]["search"];
			org.OpenGeoPortal.InstitutionInfo.institutionSpecificCss = data["config"]["institutionSpecificCss"];
			org.OpenGeoPortal.InstitutionInfo.Config = institutions;
			org.OpenGeoPortal.InstitutionInfo.homeInstitution = data["config"]["homeInstitution"];
		}
	  };
	jQuery.ajax(params);
};


org.OpenGeoPortal.InstitutionInfo.tilecacheToWMS =  function(tilingUrl){
	var institutionInfo = org.OpenGeoPortal.InstitutionInfo.getInstitutionInfo();
	for (var institution in institutionInfo){
		var currentInstitution = institutionInfo[institution].preview;
		for (var serviceType in currentInstitution){
			if (tilingUrl == currentInstitution[serviceType].serviceAddress){
				return currentInstitution.WMS.serviceAddress;
			}
		}
	}
};